package com.yangyang.ralearn.client.rlclient;

import java.util.List;

import org.springframework.stereotype.Component;

import com.yangyang.ralearn.common.JEnvironment;
import com.yangyang.ralearn.common.entity.base.AbstractDomainPredicate;
import com.yangyang.ralearn.common.exception.ErrorActionException;
import com.yangyang.ralearn.common.exception.ReachGoalException;
import com.yangyang.ralearn.message.ReqCanDoMessage;
import com.yangyang.ralearn.message.ReqCreateGroupMessage;
import com.yangyang.ralearn.message.ReqDoActionMessage;
import com.yangyang.ralearn.message.ReqObserveMessage;
import com.yangyang.ralearn.message.RespCanDoMessage;
import com.yangyang.ralearn.message.RespCreateGroupMessage;
import com.yangyang.ralearn.message.RespDoActionMessage;
import com.yangyang.ralearn.message.RespObserveMessage;
import com.yangyang.ralearn.net.BaseClientSimulatorHandler;
import com.yangyang.ralearn.net.message.MessageFactory;
import com.yangyang.ralearn.net.message.MessageId;

@Component
public class RLMultiagentHandler extends BaseClientSimulatorHandler implements
		IRLAgentHandler {

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.yangyang.ralearn.client.rlclient.multiagent.IRLAgentHandler#init(
	 * java.lang.String, java.lang.String,
	 * com.yangyang.ralearn.common.IClientEnvironmentListener)
	 */

	@Override
	public JEnvironment init(String domainFile, String problemFile) {
		this.start();
		ReqCreateGroupMessage message = (ReqCreateGroupMessage) MessageFactory
				.instance().createMessage(MessageId.REQ_CREATE_GROUP);
		message.init(domainFile, problemFile);
		RespCreateGroupMessage respMessage = (RespCreateGroupMessage) this
				.syncSendMessage(message, (Integer[]) null);
		return respMessage.getEnvironment();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.yangyang.ralearn.client.rlclient.multiagent.IRLAgentHandler#observe()
	 */
	@Override
	public List<AbstractDomainPredicate> observe() {
		ReqObserveMessage message = (ReqObserveMessage) MessageFactory
				.instance().createMessage(MessageId.REQ_OBSERVE);
		RespObserveMessage recvMessage = (RespObserveMessage) this
				.syncSendMessage(message, (Integer[]) null);
		return recvMessage.getCurrentStates();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.yangyang.ralearn.client.rlclient.multiagent.IRLAgentHandler#canDo
	 * (java.lang.String)
	 */
	@Override
	public boolean canDo(String line) {
		ReqCanDoMessage message = (ReqCanDoMessage) MessageFactory.instance()
				.createMessage(MessageId.REQ_CANDO);
		message.init(line);
		RespCanDoMessage recvMessage = (RespCanDoMessage) this.syncSendMessage(
				message, (Integer[]) null);
		return recvMessage.getResult() == 1;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.yangyang.ralearn.client.rlclient.multiagent.IRLAgentHandler#doAction
	 * (java.lang.String)
	 */
	@Override
	public void doAction(String line) throws ReachGoalException,
			ErrorActionException {
		ReqDoActionMessage message = (ReqDoActionMessage) MessageFactory
				.instance().createMessage(MessageId.REQ_DOACTION);
		message.init(line);
		RespDoActionMessage recvMessage = (RespDoActionMessage) this
				.syncSendMessage(message, (Integer[]) null);
		if (recvMessage.getResult() == 0)
			throw new ErrorActionException("error action:" + line);
		else if (recvMessage.getResult() == 2)
			throw new ReachGoalException("goal reached!");
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		// RLMultiagentHandler.instance().start();
		((IRLAgentHandler) BaseClientSimulatorHandler.instance()).init(
				"bin/domain1.pddl", "bin/problem1.pddl");
	}

	@Override
	public void nextProblem(String problemFile) {
		// TODO Auto-generated method stub

	}

}
